/**
 * Read the current value for a given HTML element.
 * @see http://getahead.org/dwr/browser/util/getvalue
 */
dwr.getValue = function(ele, options) {
    if (options == null) options = {};
    var orig = ele;
    if (typeof ele == "string") {
        ele = dwr.byId(ele);
        // We can work with names and need to sometimes for radio buttons, and IE has
        // an annoying bug where getElementById() returns an element based on name if
        // it doesn't find it by id. Here we don't want to do that, so:
        if (ele && ele.id != orig) ele = null;
    }
    var nodes = null;
    if (ele == null) {
        // Now it is time to look by name
        nodes = document.getElementsByName(orig);
        if (nodes.length >= 1) ele = nodes.item(0);
    }
    if (ele == null) {
        return "";
    }

    if (dwr._isHTMLElement(ele, "input")) {

        if (ele.type == "file") {
            return ele;
        }
        return ele.value;
    }

    return ele.innerHTML;
};

/**
 * Find the element in the current HTML document with the given id or ids
 * @see http://getahead.org/dwr/browser/util/$
 */
if (document.getElementById) {
    dwr.byId = function() {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string') {
                element = document.getElementById(element);
            }
            if (arguments.length == 1) {
                return element;
            }
            elements.push(element);
        }
        return elements;
    };
}
else if (document.all) {
    dwr.byId = function() {
        var elements = new Array();
        for (var i = 0; i < arguments.length; i++) {
            var element = arguments[i];
            if (typeof element == 'string') {
                element = document.all[element];
            }
            if (arguments.length == 1) {
                return element;
            }
            elements.push(element);
        }
        return elements;
    };
}

/**
 * @private Is the given node an HTML element (optionally of a given type)?
 * @param ele The element to test
 * @param nodeName eg "input", "textarea" - check for node name (optional)
 *         if nodeName is an array then check all for a match.
 */
dwr._isHTMLElement = function(ele, nodeName) {
  if (ele == null || typeof ele != "object" || ele.nodeName == null) {
    return false;
  }
  if (nodeName != null) {
    var test = ele.nodeName.toLowerCase();
    if (typeof nodeName == "string") {
      return test == nodeName.toLowerCase();
    }
    if (dwr._isArray(nodeName)) {
      var match = false;
      for (var i = 0; i < nodeName.length && !match; i++) {
        if (test == nodeName[i].toLowerCase()) {
          match =  true;
        }
      }
      return match;
    }
    return false;
  }
  return true;
};

/**
 * @private Array detector. Note: instanceof doesn't work with multiple frames.
 */
dwr._isArray = function(data) {
  return (data && data.join);
};